import 'package:flutter/material.dart';
import 'package:get/get.dart';
// import 'package:shared_preferences/shared_preferences.dart';

import 'author_state.dart';
import '../../routes/routes.dart';
import '../../shared/shared.dart';

class AuthorLogic extends GetxController {
  final RxString userName = '大粒达利'.obs;
  final RxString password = '123456789'.obs;
  static const String token = '895u3485y3748%^H';

  final state = AuthorState();

  final GlobalKey<FormState> genderFormKey = GlobalKey<FormState>();

  final GlobalKey<FormState> registerFormKey = GlobalKey<FormState>();
  final registerUserController = TextEditingController();
  final registerPasswordController = TextEditingController();
  final registerConfirmPasswordController = TextEditingController();

  final GlobalKey<FormState> loginFormKey = GlobalKey<FormState>();
  final loginUserController = TextEditingController();
  final loginPasswordController = TextEditingController();

  void changeGender(int value) => state.gender.value = value;

  void changeSelect() => state.select.value = !state.select.value;

  void register(BuildContext context) async {
    AppFocus.unFocus(context);
    if (registerFormKey.currentState!.validate()) {
      if (!state.select.value) {
        CommonWidget.toast('请先同意用户协议。');
        return;
      } else {
        CommonWidget.toast('用户注册成功');
        userName.value = registerUserController.text;
        password.value = registerConfirmPasswordController.text;
        Get.back();
      }
    }
  }

  void login(BuildContext context) async {
    AppFocus.unFocus(context);
    if (loginFormKey.currentState!.validate()) {
      if (loginUserController.text == userName.value &&
          loginPasswordController.text == password.value) {
        Get.toNamed(Routes.home);
      } else {
        CommonWidget.toast('用户名或密码不正确,请重新输入。');
      }
    }
  }

  @override
  void onClose() {
    super.onClose();

    registerUserController.dispose();
    registerPasswordController.dispose();
    registerConfirmPasswordController.dispose();

    loginUserController.dispose();
    loginPasswordController.dispose();
  }
}
